<!DOCTYPE html>
<html lang="pt-BR">
    <head>
        <?php
        session_start();
        include_once'bloco/head.php';
        include_once '../control/conexao.php';
        include_once '../control/valida_sessao.php';

        function __autoload($classe) {
            include_once"../model/{$classe}.class.php";
        }
        ?>
        <link href="bootstrap/css/morris.css" rel="stylesheet">
    </head>
    <body>      
        <div id="wrapper">
            <?php
            include_once'bloco/navbar.php';
            include_once '../control/valida_perfil.php';
            ?>
            <div id="page-wrapper">
                <?php include_once 'bloco/banner.php'; ?>                  
                <div class=" panel panel-success">
                    <div class="panel-heading">
                        <?php
                        $perfilDAO = new perfilDAO();
                        if (isset($_GET['id'])) {
                            $_SESSION['id'] = $_GET['id'];
                            $id_perfil = $_SESSION['id'];
                        } else {
                            $id_perfil = $_SESSION['id'];
                        }
                        $perfil = $perfilDAO->consultar($id_perfil);
                        ?>
                        <p class="panel-title"><i class = "glyphicon glyphicon-th-large"></i> Gerenciar Perfil <?php echo utf8_encode($perfil->perfil_nome) ?></p>
                    </div>
                    <div class="panel-body">
                        <div class="row">
                            <div class="col-md-5">
                                <form autocomplete="off" name="form_gerencia_menu" method="get" action="../control/gerenciar_perfil.php">
                                    <div class="alert alert-success alert-dismissable">
                                        <table class="table table-condensed table-responsive">                                            
                                            <tr>
                                                <td><strong>Menu</strong></td>
                                                <td>
                                                    <select name = "id_menu" class = "btn btn-block" required="">
                                                        <option value="">Selecione</option>
                                                        <?php
                                                        $arMenuDesvinculados = $perfilDAO->listarMenusDesvinculados($_GET['id']);
                                                        for ($c = 0; count($arMenuDesvinculados) > $c; $c++) {
                                                            ?>
                                                            <option value="<?php echo $arMenuDesvinculados[$c]->id_menu ?>"><?php echo utf8_encode($arMenuDesvinculados[$c]->menu_nome) ?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                            <input type="hidden" name="form" value="menu">
                                            <input type="hidden" name="id_perfil" value="<?php echo $_GET['id'] ?>">
                                            <input type="hidden" name="op" value="vincular" >
                                            <td><input type = "submit" class = "btn btn-success" value = "vincular"/></td>
                                            </tr>                                                    
                                        </table>
                                        <table class = "table table-condensed table-responsive table-bordered">
                                            <tr>
                                                <td align = "center"><strong>Vinculados</strong></td>
                                                <td align = "center"><strong>Desvincular</strong></td>
                                            </tr>
                                            <?php
                                            $arMenuVinculados = $perfilDAO->listarMenusVinculados($id_perfil);
                                            for ($c = 0; count($arMenuVinculados) > $c; $c++) {
                                                ?>
                                                <tr>
                                                    <td align="center"><?php echo utf8_encode($arMenuVinculados[$c]->menu_nome) ?></td>
                                                    <td align="center"><a href="../control/gerenciar_perfil.php?id_menu=<?php echo utf8_encode($arMenuVinculados[$c]->id_menu) ?>&id_perfil=<?php echo $id_perfil ?>&op=desvincular&form=menu"><i class="glyphicon glyphicon-remove-circle"></i></a></td>
                                                </tr>                                                    
                                            <?php } ?>
                                        </table>                                                
                                    </div>
                                </form>
                            </div>
                            <div class="col-md-5 col-md-offset-2">
                                <form autocomplete="off" name="form_gerencia_menu" method="get" action="../control/gerenciar_perfil.php">
                                    <div class="alert alert-success alert-dismissable">
                                        <table class="table table-condensed table-responsive">                                            
                                            <tr>
                                                <td><strong>Sub-Menu</strong></td>
                                                <td>
                                                    <select name = "id_submenu" class = "btn btn-block" required="">
                                                        <option value="">Selecione</option>
                                                        <?php
                                                        $arSubMenuDesvinculados = $perfilDAO->listarSubMenusDesvinculados($_GET['id']);
                                                        for ($c = 0; count($arSubMenuDesvinculados) > $c; $c++) {
                                                            ?>
                                                            <option value="<?php echo $arSubMenuDesvinculados[$c]->id_submenu ?>"><?php echo utf8_encode($arSubMenuDesvinculados[$c]->submenu_nome) ?></option>
                                                        <?php } ?>
                                                    </select>
                                                </td>
                                            <input type="hidden" name="form" value="submenu">
                                            <input type="hidden" name="id_perfil" value="<?php echo $_GET['id'] ?>">
                                            <input type="hidden" name="op" value="vincular" >
                                            <td><input type = "submit" class = "btn btn-success" value = "vincular"/></td>
                                            </tr>                                                    
                                        </table>
                                        <table class = "table table-condensed table-responsive table-bordered">
                                            <tr>
                                                <td align = "center"><strong>Vinculados</strong></td>
                                                <td align = "center"><strong>Desvincular</strong></td>
                                            </tr>
                                            <?php
                                            $arSubMenuVinculados = $perfilDAO->listarSubMenusVinculados($id_perfil);
                                            for ($c = 0; count($arSubMenuVinculados) > $c; $c++) {
                                                ?>
                                                <tr>
                                                    <td align="center"><?php echo utf8_encode($arSubMenuVinculados[$c]->submenu_nome) ?></td>
                                                    <td align="center"><a href="../control/gerenciar_perfil.php?id_submenu=<?php echo utf8_encode($arSubMenuVinculados[$c]->id_submenu) ?>&id_perfil=<?php echo $id_perfil ?>&op=desvincular&form=submenu"><i class="glyphicon glyphicon-remove-circle"></i></a></td>
                                                </tr>                                                    
                                            <?php } ?>
                                        </table>                                                
                                    </div>
                                </form>                                
                            </div> 
                        </div>
                        <div class="row">
                            <div align = "center">
                                <input type = "button" class = "btn btn-inverse" value = "Voltar" onclick = "history.go(-1)"/>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <?php
        include_once'bloco/scripts.php';
        ?>        
    </body>
</html>